using System;
using System.Data;
using System.Data.OleDb;
using System.Data.OracleClient;
using System.Data.SqlClient;

namespace Antu.Core.DataAccess
{
    /// <summary>
    /// 数据工厂，以获得对应的数据对象
    /// </summary>
    public class DataFactory
    {
        private const DatabaseType DefaultDbType = DatabaseType.Oracle;

        private DataFactory()
        {
        }

        /// <summary>
        /// 取得一个指定联接串的数据库访问对象实例
        /// </summary>
        /// <returns></returns>
        public static IDataAccess GetInstance()
        {
            return GetInstance(false);
        }

        /// <summary>
        /// 取得一个指定联接串的数据库访问对象实例
        /// </summary>
        /// <param name="transaction">是否带事务</param>
        /// <returns></returns>
        public static IDataAccess GetInstance(bool transaction)
        {
            return GetInstance(transaction, DatabaseType.Oracle);
        }

        /// <summary>
        /// 取得一个指定联接串的数据库访问对象实例
        /// </summary>
        /// <param name="transaction">是否带事务</param>
        /// <param name="dbType">数据库类型</param>
        /// <returns></returns>
        public static IDataAccess GetInstance(bool transaction, DatabaseType dbType)
        {
            return GetInstance(transaction, dbType, "");
        }

        /// <summary>
        /// 取得一个指定联接串的数据库访问对象实例
        /// </summary>
        /// <param name="transaction">是否带事务</param>
        /// <param name="dbType">数据库类型</param>
        /// <param name="connectionString">联接串</param>
        /// <returns></returns>
        public static IDataAccess GetInstance(bool transaction, DatabaseType dbType, string connectionString)
        {
            switch (dbType)
            {
                case DatabaseType.SqlServer:
                    return new SqlDataAccess(transaction, connectionString);

                case DatabaseType.Oracle:
                    return new OracleDataAccess(transaction, connectionString);

                case DatabaseType.Access:
                    return new OledbDataAccess(transaction, connectionString);
            }
            return null;
        }

        /// <summary>
        /// 根据对应的数据库类型返回对应的数据参数数组,对于特殊的参数类型，请直接使用对应的类创建
        /// 比如：new OracleParameter(name,OracleType.Cursor....);  
        /// </summary>
        /// <param name="dbType">数据库类型</param>
        /// <param name="count">维数</param>
        /// <returns></returns>
        public static IDataParameter[] GetParameter(DatabaseType dbType, int count)
        {
            IDataParameter[] sqlParams;
            int i;
            switch (dbType)
            {
                case DatabaseType.SqlServer:
                    sqlParams = new SqlParameter[count];
                    for (i = 0; i < count; i++)
                    {
                        sqlParams[i] = new SqlParameter();
                    }
                    return sqlParams;

                case DatabaseType.Oracle:
                    sqlParams = new OracleParameter[count];
                    for (i = 0; i < count; i++)
                    {
                        sqlParams[i] = new OracleParameter();
                    }
                    return sqlParams;

                case DatabaseType.Access:
                    sqlParams = new OleDbParameter[count];
                    for (i = 0; i < count; i++)
                    {
                        sqlParams[i] = new OleDbParameter();
                    }
                    return sqlParams;
            }
            return null;
        }
    }
}

